<template>
  <div>
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="产品">
        <el-select v-model="form.goodsID" placeholder="请选择产品编号">
          <el-option
            v-for="i in options"
            :label="i.goodsID"
            :value="i.goodsID"
            :key="i.goodsID"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="进货员">
        <el-select v-model="form.employeeID" placeholder="请选择进货员编号">
          <el-option
            v-for="i in employees"
            :label="i.employeeID"
            :value="i.employeeID"
            :key="i.employeeID"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="购买数量">
        <el-input-number
          v-model="num"
          @change="handleChange"
          :min="1"
          :max="10"
          label="描述文字"
        >
        </el-input-number>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">立即创建</el-button>
        <el-button @click="reset">取消</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" style="width: 100%" height="300">
      <el-table-column fixed prop="purcahseID" label="进货单号" width="100">
      </el-table-column>
      <el-table-column prop="purchaseDate" label="进货时间" width="220">
      </el-table-column>
      <el-table-column prop="goodsID" label="产品编号" width="100">
      </el-table-column>
      <el-table-column prop="supplierID" label="生产商编号" width="100">
      </el-table-column>
      <el-table-column prop="purchasePrice" label="进价" width="100">
      </el-table-column>
      <el-table-column prop="purchaseAmount" label="购买数量" width="100">
      </el-table-column>
      <el-table-column prop="employeeID" label="员工号" width="100">
      </el-table-column>
      <el-table-column prop="totalMoney" label="总价"> </el-table-column>
    </el-table>
  </div>
</template>

<script>
import axios from "axios";
import qs from "qs";
export default {
  data() {
    return {
      form: {
        goodsID: "",
        employeeID: "",
      },
      tableData: [],
      num: 1,
      options: [],
      employees: [],
    };
  },
  methods: {
    onSubmit() {
    
      let formatDate = function (date) {
        let y = date.getFullYear();
        let m = date.getMonth() + 1;
        m = m < 10 ? "0" + m : m;
        let d = date.getDate();
        d = d < 10 ? "0" + d : d;
        return y + "-" + m + "-" + d;
      };
      
      let date=formatDate(new Date())
      axios({
        url: "http://localhost:3000/buySome",
        method: "post",
        data: qs.stringify({
          gID: this.form.goodsID,
          eID: this.form.employeeID,
          date:date,
          num:this.num
        }),
      });
      this.$message({
          message: '添加成功！',
          type: 'success'
        })
      location.reload()
    },
    handleChange(value) {
      console.log(value);
    },
    getOpitons() {
      return axios.get("http://localhost:3000/getOptions");
    },
    getEmployee() {
      return axios.get("http://localhost:3000/getEmployeeID");
    },
    getTableData() {
      return axios.get("http://localhost:3000/buy");
    },
    reset() {
      this.$router.push("/order");
    },
  },
  mounted() {
    axios
      .all([this.getOpitons(), this.getTableData(), this.getEmployee()])
      .then(
        axios.spread((res1, res2, res3) => {
          this.tableData = res2.data;
          this.options = res1.data;
          this.employees = res3.data;
        })
      );
  },
};
</script>

<style>
</style>